<?php
namespace plugins\admin_journal\controller;

use cmf\controller\PluginAdminBaseController;//引入此类
use think\Db;

class AdminIndexController extends PluginAdminBaseController
{

    protected function _initialize()
    {
        parent::_initialize();
        $adminId = cmf_get_current_admin_id();//获取后台管理员id，可判断是否登录
        if (!empty($adminId)) {
            $this->assign("admin_id", $adminId);
        }
    }

    /**
     * 操作日志
     * @adminMenu(
     *     'name'   => '操作日志',
     *     'parent' => 'admin/Plugin/default',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 1000,
     *     'icon'   => '',
     *     'remark' => '操作日志',
     *     'param'  => ''
     * )
     */
    public function index()
    {   
        $data = $this->request->param();

        $where = 1;
        if (isset($data['user_name']) && !empty($data['user_name'])) {
            $where .= " and user_name like '%".$data['user_name']."%'";
        }
        if (isset($data['start_time']) && !empty($data['start_time'])) {
            $where .= " and time >= ".strtotime($data['start_time']);
        }
        if (isset($data['end_time']) && !empty($data['end_time'])) {
            $where .= " and time <= ".strtotime($data['end_time']);
        }
        if (isset($data['type']) && !empty($data['type'])) {
            $where .= " and type = ".$data['type'];
        }
        if (isset($data['module_sign']) && !empty($data['module_sign'])) {
            $where .= " and module_sign = '".$data['module_sign']."'";
        }


        $journal = Db::name('admin_journal')->where($where)->order('id','desc')->paginate(20);

        //计算数量
        $num = Db::name('admin_journal')->where($where)->order('id','desc')->count();

        //读取模块名称
        $plugin = Db::name('plugin')->field('name,title')->select();

        $this->assign('plugin',$plugin);

        $this->assign('journal',$journal);

        $journal->appends($data);

        $this->assign('page',$journal->render());

        $this->assign('data',$data);

        $this->assign('num',$num);

        return $this->fetch();
    }




    /*
     * 日志全部删除
     */
    public function allDel(){

        Db::name('admin_journal')->where('id','<>',0)->delete();

        $this->success('删除成功');
    }



    /*
     * 一月前数据删除
     */
    public function oneYueQianDel(){

        $yiyueqian = strtotime('-30day'); //一月前时间

        Db::name('admin_journal')->where('time','<',$yiyueqian)->delete();

        $this->success('删除成功');

    }


}
